#! /bin/sh


##########################################################################
###   ###   ###   ###   ###   ###   ###   ###   ###   ###   ###   ###   ###
###########################################################################
# #
# # PRELUDE - Standard startup code for loading mulle-bashfunctions
# #
###

#
# Figure out MULLE_BASHFUNCTIONS_LIBEXEC_DIR on this machine if needed.
#
if ! [ ${MULLE_BASHFUNCTIONS_LIBEXEC_DIR+x} ]
then
   MULLE_BASHFUNCTIONS_LIBEXEC_DIR="`mulle-bashfunctions libexec-dir`" || exit 1
   export MULLE_BASHFUNCTIONS_LIBEXEC_DIR
fi

#
# The booter reruns the executable either with bash or zsh, whatever the
# system provides. On the second run --no-auto-shell will be set first.
#
if [ "${1:-}" != --no-auto-shell ]
then
   . "${MULLE_BASHFUNCTIONS_LIBEXEC_DIR}/mulle-boot.sh" || exit 1
else
   shift    # get rid of --no-auto-shell
fi


#
# Include mulle-bashfunctions.sh         : default functions
#         mulle-bashfunctions-none.sh    : almost no functions
#         mulle-bashfunctions-minimal.sh : few functions for basic scripts
#         mulle-bashfunctions-all.sh     : everything, even obscure functions
#
# Do this first to get aliases
#
. "${MULLE_BASHFUNCTIONS_LIBEXEC_DIR}/mulle-bashfunctions.sh" || exit 1

###
# # Now we are in bash or zsh with mulle-bashfunctions loaded
# #
###########################################################################
###   ###   ###   ###   ###   ###   ###   ###   ###   ###   ###   ###   ###
###########################################################################

main()
{
   local OPTION_ADD_MULLE

   #
   # simple option handling
   #
   while [ $# -ne 0 ]
   do
      if options_technical_flags "$1"
      then
         shift
         continue
      fi

      case "$1" in
         --add-mulle)
            OPTION_ADD_MULLE='YES'
         ;;
      esac
      # just ignore
      shift
   done

   options_setup_trace "${MULLE_TRACE}" && set -x

      # Add Mulle Foundation

   exekutor "${MULLE_SOURCETREE:-mulle-sourcetree}" \
                     ${MULLE_TECHNICAL_FLAGS} \
                     ${MULLE_SOURCETREE_FLAGS:-} \
                  add \
                     --if-missing \
                     --nodetype '${FOUNDATION_NODETYPE:-tar}' \
                     --marks no-singlephase,no-platform-darwin,no-require-os-darwin \
                     --branch '${FOUNDATION_BRANCH}' \
                     --tag '${FOUNDATION_TAG:-latest}' \
                     --url '${FOUNDATION_URL:-https://github.com/MulleFoundation/Foundation/archive/${MULLE_TAG}.tar.gz}' \
                     'Foundation' || exit 1

   if [ "${PROJECT_TYPE}" = "executable" ]
   then
      exekutor "${MULLE_SOURCETREE:-mulle-sourcetree}" \
                     ${MULLE_TECHNICAL_FLAGS} \
                     ${MULLE_SOURCETREE_FLAGS:-} \
                  add \
                     --if-missing \
                     --nodetype '${FOUNDATION__STARTUP_NODETYPE:-tar}' \
                     --marks no-dynamic-link,no-intermediate-link,no-header,no-cmake-dependency,no-cmake-loader,no-cmake-searchpath,no-platform-darwin,no-require-os-darwin \
                     --branch '${FOUNDATION__STARTUP_BRANCH}' \
                     --tag '${FOUNDATION__STARTUP_TAG:-latest}' \
                     --url '${FOUNDATION__STARTUP_URL:-https://github.com/MulleFoundation/Foundation-startup/archive/${MULLE_TAG}.tar.gz}' \
                     'Foundation-startup' || exit 1
   fi

   #
   # Modify Mulle Foundation
   #
   exekutor "${MULLE_SOURCETREE:-mulle-sourcetree}" \
                  ${MULLE_TECHNICAL_FLAGS} \
                  ${MULLE_SOURCETREE_FLAGS:-} \
               unmark -e \
                  Foundation platform-darwin || exit 1
   exekutor "${MULLE_SOURCETREE:-mulle-sourcetree}" \
                  ${MULLE_TECHNICAL_FLAGS} \
                  ${MULLE_SOURCETREE_FLAGS:-} \
               unmark -e \
                  Foundation require-os-darwin || exit 1

   if [ "${PROJECT_TYPE}" = "executable" ]
   then
      exekutor "${MULLE_SOURCETREE:-mulle-sourcetree}" \
                     ${MULLE_TECHNICAL_FLAGS} \
                     ${MULLE_SOURCETREE_FLAGS:-} \
                  unmark -e \
                     Foundation-startup platform-darwin || exit 1
      exekutor "${MULLE_SOURCETREE:-mulle-sourcetree}" \
                     ${MULLE_TECHNICAL_FLAGS} \
                     ${MULLE_SOURCETREE_FLAGS:-} \
                  unmark -e \
                     Foundation-startup require-os-darwin || exit 1
   fi

   # Apple Foundation
   exekutor "${MULLE_SOURCETREE:-mulle-sourcetree}" \
                  ${MULLE_TECHNICAL_FLAGS} \
                  ${MULLE_SOURCETREE_FLAGS:-} \
               add \
                  --if-missing \
                  --nodetype 'none' \
                  --marks 'no-all-load,only-framework,no-build,no-cmake-inherit,no-delete,no-dependency,no-fs,no-share,no-update,only-cmake-platform-darwin,only-platform-darwin' \
                  --userinfo 'include=Foundation/Foundation.h
aliases=Foundation' \
                  'AppleFoundation' || exit 1

   exekutor "${MULLE_SOURCETREE:-mulle-sourcetree}" \
                  ${MULLE_TECHNICAL_FLAGS} \
                  ${MULLE_SOURCETREE_FLAGS:-} \
               add \
                  --if-missing \
                  --nodetype '${OBJC_COMPAT_NODETYPE:-tar}' \
                  --marks 'no-all-load,no-import,no-link' \
                  --url '${OBJC_COMPAT_URL:-https://github.com/MulleFoundation/objc-compat/archive/${MULLE_TAG}.tar.gz}' \
                  --branch '${OBJC_COMPAT_BRANCH}' \
                  --tag '${OBJC_COMPAT_TAG:-latest}' \
                  objc-compat

   exekutor "${MULLE_ENV:-mulle-env}" environment \
                  --scope global \
               set \
                  MULLE_SOURCETREE_USE_PLATFORM_MARKS_FOR_FETCH "YES" || exit 1
}


main "$@"
